// *** LICENSE HEADER ***
// Filename: ./playerlist.h
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#ifndef NODEWARS_PLAYER_LIST_H
#define NODEWARS_PLAYER_LIST_H

#include <SDL.h>
#include <GL/gl.h>
#include <cfloat>
#include <cstring>
#include <cmath>
#include "Ieventhandler.h"
#include "player.h"
#include "game.h"
#include "gui.h"
#include "colorviewer.h"
#include "statusbar.h"
#include "particleemitter.h"
#include "lightmanager.h"
#include "helperfunctions.h"
#include "imagefunctions.h"
#include "definitions.h"
#include "node.h"
#include "toggle.h"

class connector;
class game;
class player;

//Enumeration of various modes for user interaction
enum modeType{
    MT_SELECT,
    MT_ADD_SELECT_CONNECTIONS,
    MT_ADD,
    MT_ATTACK,
    MT_DEFEND,
    MT_TAKEOVER,
    MT_DISSINTEGRATE
};

class playerList: public IeventHandler {
    public:
        playerList(game* newParent);
        ~playerList();

        //Set the connector pointer
        void SetConnector(connector* newConnector);

        static void LoadTextures();
        static void UnloadTextures();

        //Set the user interaction mode
        void SetMode(modeType t);

        //Delete all players
        void DeletePlayers();
        //Create new default players
        void CreateDefaultPlayers();
        //Increase player number by one; create a new player
        void IncPlayers();
        //Decrease player number by one; destroy the last player
        void DecPlayers();
        //Sets next player as current. Returns true if the client was allowed to make a new round
        bool NextPlayer(bool fromNet = false);
        //Create playerNum players
        int SetPlayerNum(int playerNum);
        //Return the current player number
        int GetPlayerNum();
        //Return the active player handle
        int GetActivePlayer();
        //Return the active player pointer
        player* GetActivePlayerPointer();
        //Return the specified player pointer
        player* GetPlayerPointer(int playerId);
        //Returns the round number
        int GetRound();

        //Connects nodes in the node stack, if the player has enough money to connect them
        void ConnectNodesInStack();

        //Deselects all selected nodes of all players
        void DeselectAllNodes();

        //Draws all players
        void Draw();
        //Sets the cursor position
        void SetPickingCursorPosition(float x, float y);

        //Sets the player type of the specified client's players to PT_LOCAL
        void ClaimClientsPlayers(int clientNumber);

        //Returns true if the game has ended
        bool IsEndGame();

        //IeventHandler methods
        void MouseClickEvent(int xCoordinate, int yCoordinate, bool right = false);
        void MouseReleaseEvent(int xCoordinate, int yCoordinate, bool right = false);
        void MouseMoveEvent(int newX, int newY, int relX, int relY);
        void KeyPressEvent(SDLKey key, char c);
        void KeyReleaseEvent(SDLKey key);
    private:
        //Current number of players
        int playerNum;
        //Active player handle
        int activePlayer;
        //Player array
        player* players[8];
        //Helper buffer
        char buffer[64];
        //CUrsor x and y positions
        float curX, curY;

        //Texture used for highlighting nodes in nodestack
        static GLuint nodestackTexture;

        //Default initial player data
        static GLubyte defaultColors[8][3];
        static float defaultStartnodes[8][2];

        //User interaction mode
        modeType mode;
        //Current round
        int round;
        //End game flag
        bool endGame;

        //Pointers to other game subsystems
        game* parent;
        connector* theConnector;

        //Stack of selected nodes
        node** nodeStack;
        //Size of the stack
        int nodeStackSize;
        //Pointer to the stack
        int nodeStackPointer;

        //Node stack helper methods
        void ReallocateNodeStack();
        void AddNodeToStack(node* n);
        bool NodeIsInStack(node* n);

        //Sets the node info labels in the game gui to the selected node's data of the given player
        void SetInfoLabelsForClosestNode(player* pl);
};

#include "connector.h"

#endif
